package component.fulltextsearch.web.control;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;

import component.fulltextsearch.constant.SearchConstant;
import component.fulltextsearch.entity.UserDocument;
import component.fulltextsearch.index.IIndexOperate;
import component.fulltextsearch.index.SimpleIndexOperate;
import component.fulltextsearch.util.SearchConfigUtil;
import component.fulltextsearch.web.action.SearchAction;

/**
 * 搜索控制器，
 * 接收搜索请求
 *
 */
public class SearchServlet extends HttpServlet{

	private SearchAction searchAction = new SearchAction();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if ("doSearch".equals(action)){
			searchAction.doSearch(request, response);
			request.getRequestDispatcher(SearchConfigUtil.getSearchPath() + "/simpleResult.jsp").forward(request, response);
		}else if("toList".equals(action)){
			searchAction.toList(request, response);
			request.getRequestDispatcher(SearchConfigUtil.getSearchPath() + "/listIndex.jsp").forward(request, response);
		}else if("doDelete".equals(action)){
			searchAction.doDelete(request, response);
			request.getRequestDispatcher(SearchConfigUtil.getSearchPath() + "/listIndex.jsp").forward(request, response);
		}else if("doDeleteAll".equals(action)){
			searchAction.doDeleteAll(request, response);
			request.getRequestDispatcher(SearchConfigUtil.getSearchPath() + "/listIndex.jsp").forward(request, response);
		}else if("toEdit".equals(action)){
			searchAction.toEdit(request, response);
			request.getRequestDispatcher(SearchConfigUtil.getSearchPath() + "/editIndex.jsp").forward(request, response);
		}else if("doSave".equals(action)){
			searchAction.doSave(request, response);
			searchAction.toEdit(request, response);
			request.getRequestDispatcher(SearchConfigUtil.getSearchPath() + "/editIndex.jsp").forward(request, response);
		}
	}
	
	public void init() throws ServletException {		
		super.init();		
	}

}
